package com.muyu.danmo.service.impl;


import com.muyu.common.core.domain.Result;
import com.muyu.danmo.domain.DanmoGalleryDetailsLikes;
import com.muyu.danmo.mapper.DanmoGalleryDetailsLikesMapper;
import com.muyu.danmo.service.IDanmoGalleryDetailsLikesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 画廊点赞Service业务层处理
 *
 * @author yangle
 * @date 2024-10-23
 */
@Service
public class DanmoGalleryDetailsLikesServiceImpl implements IDanmoGalleryDetailsLikesService
{
    @Autowired
    private DanmoGalleryDetailsLikesMapper danmoGalleryDetailsLikesMapper;

    /**
     * 查询画廊点赞
     *
     * @param id 画廊点赞主键
     * @return 画廊点赞
     */
    @Override
    public DanmoGalleryDetailsLikes selectDanmoGalleryDetailsLikesById(Long id)
    {
        return danmoGalleryDetailsLikesMapper.selectDanmoGalleryDetailsLikesById(id);
    }

    /**
     * 查询画廊点赞列表
     *
     * @param danmoGalleryDetailsLikes 画廊点赞
     * @return 画廊点赞
     */
    @Override
    public List<DanmoGalleryDetailsLikes> selectDanmoGalleryDetailsLikesList(DanmoGalleryDetailsLikes danmoGalleryDetailsLikes)
    {
        return danmoGalleryDetailsLikesMapper.selectDanmoGalleryDetailsLikesList(danmoGalleryDetailsLikes);
    }

    /**
     * 新增画廊点赞
     *
     * @param danmoGalleryDetailsLikes 画廊点赞
     * @return 结果
     */
    @Override
    public int insertDanmoGalleryDetailsLikes(DanmoGalleryDetailsLikes danmoGalleryDetailsLikes)
    {
        return danmoGalleryDetailsLikesMapper.insertDanmoGalleryDetailsLikes(danmoGalleryDetailsLikes);
    }

    /**
     * 修改画廊点赞
     *
     * @param danmoGalleryDetailsLikes 画廊点赞
     * @return 结果
     */
    @Override
    public int updateDanmoGalleryDetailsLikes(DanmoGalleryDetailsLikes danmoGalleryDetailsLikes)
    {
        return danmoGalleryDetailsLikesMapper.updateDanmoGalleryDetailsLikes(danmoGalleryDetailsLikes);
    }

    /**
     * 批量删除画廊点赞
     *
     * @param ids 需要删除的画廊点赞主键
     * @return 结果
     */
    @Override
    public int deleteDanmoGalleryDetailsLikesByIds(Long[] ids)
    {
        return danmoGalleryDetailsLikesMapper.deleteDanmoGalleryDetailsLikesByIds(ids);
    }

    /**
     * 删除画廊点赞信息
     *
     * @param id 画廊点赞主键
     * @return 结果
     */
    @Override
    public int deleteDanmoGalleryDetailsLikesById(Long id)
    {
        return danmoGalleryDetailsLikesMapper.deleteDanmoGalleryDetailsLikesById(id);
    }

    @Override
    public Result<String> likeGalleryDetails(Long userId, Long detailsId) {
        if (danmoGalleryDetailsLikesMapper.existsByUserIdAndGalleryDetailsId(userId, detailsId)) {
            return Result.success( "用户已点赞");
        }

        DanmoGalleryDetailsLikes danmoGalleryDetailsLikes = new DanmoGalleryDetailsLikes();
        danmoGalleryDetailsLikes.setUserId(userId);
        danmoGalleryDetailsLikes.setDetailsId(detailsId);
        insertDanmoGalleryDetailsLikes(danmoGalleryDetailsLikes);
        danmoGalleryDetailsLikesMapper.updateAmountPraise(detailsId);
        return Result.success("点赞成功");
    }

    @Override
    public Long getLikesCount(Long detailsId) {
        return danmoGalleryDetailsLikesMapper.countByDetailsId(detailsId);
    }
}
